package com.howard.jms;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

/**
 * LogSender implement
 *
 * @author Howard.He
 * @version 1.0
 *
 */
@Component
public class LogSenderImpl implements LogSender {

	private JmsTemplate jmsTemplate;
	private JmsTemplate jmsQueueTemplate;

	public void setJmsQueueTemplate(JmsTemplate jmsQueueTemplate) {
		this.jmsQueueTemplate = jmsQueueTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	@Override
	public void send(final String message) {
		if (StringUtils.isBlank(message)) {
			throw new IllegalArgumentException("message is null or empty");
		}
		// new MessageCreator
		MessageCreator creator = new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage = session.createTextMessage();
				textMessage.setText(message);
				return textMessage;
			}
		};
		jmsTemplate.send(creator);
		jmsQueueTemplate.send(creator);
	}

}
